package com.xuexiang.xuidemo.dialog;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.XulPopupItem;
import com.xuexiang.xuidemo.provider.XulPopListProvider;
import com.xuexiang.xuidemo.util.TextUtils;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class XulPoPupDialog  extends PopupDialog {
    private Context mContext;
    private PopDestroyedListener mPopDestroyedListener;
    private  Component component;
    private ListContainer container;
//    private String mtvMsg;
    private Text tv_Msg;
    private XulPopListProvider xulPopListProvider;
    public XulPoPupDialog(Context context, Component contentComponent,String  mtvMsg) {
        super(context, contentComponent);
        this.mContext=context;
//        this.mtvMsg=mtvMsg;
        component= LayoutScatter.getInstance(context).parse(ResourceTable.Layout_xui_popup_layout, null, true);
        setmTvMsg(mtvMsg);
        setCustomComponent(component);

    }

    public void setmTvMsg(String mtvMsg){
            container=(ListContainer) component.findComponentById(ResourceTable.Id_list_container);
            tv_Msg=(Text) component.findComponentById(ResourceTable.Id_tv_Msg);
            if(!TextUtils.isEmpty(mtvMsg)){
                tv_Msg.setVisibility(Component.VISIBLE);
                tv_Msg.setText(mtvMsg);
            }else{
                container.setVisibility(Component.VISIBLE);
                initListContainer();
            }
    }

    public void initListContainer(){
        xulPopListProvider=new XulPopListProvider(mContext,getData());
        container.setItemProvider(xulPopListProvider);
      //  container.setItemClickedListener();
    }

    public  void setPopItemClickedListener(ListContainer.ItemClickedListener itemClickedListener ){
        container.setItemClickedListener(itemClickedListener);
    };

    public List<XulPopupItem> getData(){
        ArrayList<XulPopupItem>  mList=new ArrayList<>();
        for(int i=1;i<=5;i++){
            mList.add(new XulPopupItem("Item"+i));
        }
        return mList;
    };
    protected void onDestroy() {
        super.onDestroy();
        if(mPopDestroyedListener!=null){
            mPopDestroyedListener.onPopDestroy();
        }
    }

    public void setDestroyedListener(XulPoPupDialog.PopDestroyedListener popDestroyedListener) {
        this.mPopDestroyedListener = popDestroyedListener;
    }


    public interface PopDestroyedListener {
        void onPopDestroy();
    }
}
